﻿/*

  The contents of this file are subject to the Mozilla Public License Version
  1.1 (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at 
  
           http://www.mozilla.org/MPL/ 
  
  Software distributed under the License is distributed on an "AS IS" basis,
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  for the specific language governing rights and limitations under the License. 
  
  The Original Code is PEGAS Framework.
  
  The Initial Developer of the Original Code is
  ALCARAZ Marc (aka eKameleon)  <ekameleon@gmail.com>.
  Portions created by the Initial Developer are Copyright (C) 2004-2009
  the Initial Developer. All Rights Reserved.
  
  Contributor(s) :
  
*/
 
package pegas.transitions 
{
    /**
     * The TweenArray class interpolate a collection of numeric values defines in two Arrays (begin and finish).
     * <p><b>Example :</b></p>
     * <pre class="prettyprint">
     * package examples 
     * {
     *     import pegas.transitions.TweenArray;
     *     import pegas.transitions.easing.Bounce;
     *     
     *     import system.events.ActionEvent;
     *     
     *     import flash.display.Sprite;
     *     
     *     public class ExampleTweenArray extends Sprite
     *     {
     *         public function ExampleTweenArray()
     *         {
     *             var begin:Array  = [  0 ,  10 ,  20 ,  30 ] ;
     *             var finish:Array = [ 10 , 100 , 200 , 300 ] ;
     *             
     *             tween = new TweenArray ( begin, finish, Bounce.easeOut, 24, false, true ) ;
     *             
     *             tween.addEventListener( ActionEvent.CHANGE , change ) ;
     *             tween.run() ;
     *         }
     *         
     *         public var tween:TweenArray ;
     *         
     *         public function change( e:ActionEvent ):void
     *         {
     *             trace( tween.change ) ;
     *         }
     *     }
     * }
     * </pre>
     */
    public class TweenArray extends TweenUnit
    {
        /**
         * Creates a new TweenArray instance.
         * @param begin The first Array used to interpolate a collection of numeric values.
         * @param finish The second Array used to interpolate a collection of numeric values.
         * @param easing The easing equation reference.
         * @param duration A number indicating the length of time or number of frames for the tween motion.
         * @param useSeconds Indicates if the duration is in seconds.
         * @param auto Run the tween automatically.
         */
        public function TweenArray( begin:Array = null , finish:Array = null , easing:* = null , duration:Number = 0 , useSeconds:Boolean = false , auto:Boolean = false )
        {
            this.begin  = begin  ;
            this.finish = finish ;
            super( easing , duration, useSeconds, auto ) ;
        }
        
        /**
         * Determinates the first Array used to interpolate a collection of numeric values.
         */
        public function get begin():Array
        {
            return _begin ;
        }
        
        /**
         * @private
         */
        public function set begin( ar:Array ):void
        {
            _begin = ar ;
           _changed = true ;
        }
        
        /**
         * This Array contains all numeric values during the interpolation between the "begin" and "finish" arrays.
         */
        public var change:Array ;
        
        /**
         * Determinates the second Array used to interpolate a collection of numeric values.
         */
        public function get finish():Array
        {
            return _finish ;
        }
        
        /**
         * @private
         */
        public function set finish( ar:Array ):void
        {
            _finish = ar ;
            _changed = true ;
        }
        
        /**
         * Returns a shallow copy of this Tween object.
         * @return a shallow copy of this Tween object.
         */
        public override function clone():* 
        {
            return new TweenArray( _begin , _finish , easing, duration, useSeconds) ;
        }
        
        /**
         * Update the current Tween in the time.
         */
        public override function update():void 
        {
            if ( _changed )
            {
                _changed = false ;
                if ( _begin == null || _begin.length == 0 )
                {
                    throw new Error( this + " update failed, the begin property not must be null or empty.") ;
                }
                if ( _finish == null || _finish.length == 0 )
                {
                    throw new Error( this + " update failed, the finish property not must be null or empty.") ;
                }
                _size = Math.min( _begin.length , _finish.length ) ;
                change = new Array( _size ) ;
            }
            for( _count = 0 ; _count < _size ; _count++ )
            {
                change[_count] = _easing( _time, _begin[_count] , _finish[_count] - _begin[_count] , _duration ) ;
            }
            notifyChanged() ;
        }
        
        /**
         * @private
         */
        protected var _begin:Array ;
        
        /**
         * @private
         */
        protected var _changed:Boolean ;
        
        /**
         * @private
         */
        protected var _finish:Array ;
        
        /**
         * @private
         */
        private var _count:int ;
        
        /**
         * @private
         */
        private var _size:int ;
    }
}
